package de.listrong.da.main;

import gate.Corpus;
import gate.Factory;
import gate.creole.ontology.Ontology;
import gate.creole.tokeniser.DefaultTokeniser;
import gate.creole.splitter.SentenceSplitter;
import gate.creole.gazetteer.OntoGazetteer;
import gate.creole.Transducer;
import gate.creole.SerialAnalyserController;
import gate.creole.ExecutionException;

import java.net.URL;
import java.util.List;

import org.apache.log4j.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: Qiang
 * Date: 10.11.2008
 * Time: 10:47:49
 */
public class GateProcessBuilder {
    public static Logger myLogger = Logger.getLogger(GateProcessBuilder.class);
    
    private Corpus docCorpus = null;
    private List<String> fileList;
    private boolean hasGUI;
    private SerialAnalyserController controller;

    /* constructor */
    public GateProcessBuilder(List<String> fileList, boolean hasGUI) {
        this.fileList = fileList;
        this.hasGUI = hasGUI;
    }

    /* build a special GATE corpus pipeline application */
    public void build() throws Exception {
		// GATE initial
		GateProcess.init(hasGUI);

		// create language resources
		docCorpus = GateProcess.loadDocIntoCorpus(fileList);

		URL ontoURL = new URL("file:/e:/ontologies/MedOnto.owl");
		String defaultNS = "http://www.test.com/MedOnto.owl#";
		Ontology ontology = GateProcess.loadOntology(ontoURL, defaultNS);

		// create process resource for annotation extraction
		DefaultTokeniser tokeniser = GateProcess.loadTokeniser();
		SentenceSplitter splitter = GateProcess.loadSentenceSplitter();
		OntoGazetteer ontoGazetteer = GateProcess.loadOntoGazetteer();
		Transducer japeTransducer = GateProcess.loadJapeTransducer(ontology);
        Transducer japeTransducer2 = GateProcess.loadJapeTransducerNoOnto();

		controller = (SerialAnalyserController) Factory
				.createResource("gate.creole.SerialAnalyserController");
		controller.add(tokeniser);
		controller.add(splitter);
		controller.add(ontoGazetteer);
		controller.add(japeTransducer);
        controller.add(japeTransducer2);
		controller.setCorpus(docCorpus);

        myLogger.info("build a GATE application sucessful");
    }

    /* excute the GATE application*/
    public void excute() {
		// execute the gate application for basic analyst
		try {
			controller.execute();
		} catch (ExecutionException e) {
			e.printStackTrace();
			myLogger.error("controller exectue error");
		}
        myLogger.info("excute GATE application successful");
    }

    /* getter and setter methodes*/
    public void setDocumentCorpus(Corpus docCorpus) {
        this.docCorpus = docCorpus;
    }
    public Corpus getDocumentCorpus() {
        return docCorpus;
    }
}
